#!/usr/bin/env python3
"""
you call logging.{level}(message) to show the message in console.

You can configure a logger that prints to the console and another logger
that sends the logs to a file, has a different logging level and is specific
to a given module.
"""
import logging
import os
import sys
from logging.handlers import RotatingFileHandler

if not os.path.exists('logs'):
    os.mkdir('logs')

# Log file location
log_name = __name__.split('.')[0]
logfile = os.path.join('logs', log_name + '.log')

# Gets or creates a logger
# Define your own logger name
logger = logging.getLogger(log_name + "_logger")
# Set default logging level to DEBUG
logger.setLevel(logging.DEBUG)

# create a console handler
# and define a custom log format, set its log level to DEBUG
print_format = logging.Formatter('%(levelname)-8s %(name)-12s %(message)s')
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.ERROR)
console_handler.setFormatter(print_format)

# create a log file handler
rotating_log_format = logging.Formatter('[%(asctime)s] %(levelname)-8s '
                                        '%(name)-12s %(message)s '
                                        '[in %(pathname)s:%(lineno)d]')
rotating_file_handler = RotatingFileHandler(filename=logfile, maxBytes=10240,
                                            backupCount=3, encoding='gbk')
rotating_file_handler.setLevel(logging.DEBUG)
rotating_file_handler.setFormatter(rotating_log_format)

# Add fille handlers to the logger
logger.addHandler(rotating_file_handler)
logger.addHandler(console_handler)
